package com.liang.book.service;

import com.liang.book.enums.StatusEnums;
import com.liang.book.mapper.BookMapper;
import com.liang.book.model.BookInfo;
//import com.liang.book.dao.BookDao;
import com.liang.book.model.PageResult;
import com.liang.book.model.RequestPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class BookService {
//    BookDao bookDao = new BookDao();
//    @Autowired
//    private  BookDao bookDao;
    @Autowired
    private BookMapper bookMapper;
//    public List<BookInfo> getList() {
//        List<BookInfo> bookInfos = bookDao.mockData();
//        for (BookInfo bookInfo : bookInfos) {
//            if (bookInfo.getStatus() == 1) {
//                bookInfo.setStateCN("可借阅");
//            } else {
//                bookInfo.setStateCN("已借出");
//            }
//        }
//        return bookInfos;
//    }

    public Integer addBook(BookInfo bookInfo) {
      return bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(RequestPage requestPage) {
//        long start = System.currentTimeMillis();
        //1。记录
        Integer count = bookMapper.count();
        int offset = (requestPage.getCurrentPage()-1) * requestPage.getPageSize();
//        List<BookInfo> bookInfos = bookMapper.getListByPage(offset,requestPage.getPageSize());

        List<BookInfo> bookInfos = bookMapper.getListByPage(requestPage.getOffset(),requestPage.getPageSize());
//        for (BookInfo bookInfo : bookInfos) {
//            if (bookInfo.getStatus() == 1) {
//                bookInfo.setStateCN("可借阅");
//            } else {
//                bookInfo.setStateCN("已借出");
//            }
//        long end = System.currentTimeMillis();
        for (BookInfo bookInfo : bookInfos) {
            bookInfo.setStateCN(StatusEnums.getNameByCode(bookInfo.getStatus()).getName());
        }
//        log.info("getListByPage 耗时： " + (end - start) +"ms" );
        return new PageResult<>(bookInfos,count,requestPage);
//        PageResult<BookInfo> result = new PageResult<>();
//        result.setItems(bookInfos);
//        result.setCount(count);
//        return result;
    }


    public Integer updateBook(BookInfo bookinfo) {
       return bookMapper.updateBook(bookinfo);
    }

    public BookInfo getBookInfo(Integer bookId) {
        return bookMapper.getBookInfo(bookId);
    }

    public Integer deleteBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
